if exists("s:loaded")
    finish
endif
let s:loaded = 1

if has("multi_byte") 
    " UTF-8 编码 
    set encoding=utf-8 
    set termencoding=utf-8 
    set formatoptions+=mM 
    set fencs=ucs-bom,utf-8,gbk,latin1
    if v:lang =~? '^/(zh/)/|/(ja/)/|/(ko/)' 
        set ambiwidth=double 
    endif 
else 
    echoerr "Sorry, this version of (g)vim was not compiled with +multi_byte" 
endif
set number
set tabstop=4
set expandtab
set cursorline
set shiftwidth=4
set softtabstop=4
set nobackup	   " do not keep a backup file, use versions instead
set undofile	   " keep an undo file (undo changes after closing)
set autochdir
set noshowmode
set laststatus=2
set colorcolumn=80
set winminwidth=2
set viminfofile=$VIM/vimfiles/_viminfo
set updatetime=100
set signcolumn=number

let s:root_markers = ['.root', '.svn', '.git', '.hg', '.project']

" 指定插件目录
silent! call plug#begin('$VIM/vimfiles/plugged')

" " 代码片段

" 配色
Plug 'tomasr/molokai'
    let g:molokai_original = 1
    let g:rehash256 = 1
Plug 'NLKNguyen/papercolor-theme'

" 自动索引
Plug 'ludovicchabant/vim-gutentags'
    " let g:gutentags_trace = 1
    " gutentags 搜索工程目录的标志，碰到这些文件/目录名就停止向上一级目录递归
    let g:gutentags_project_root = s:root_markers

    " 所生成的数据文件的名称
    let g:gutentags_ctags_tagfile = '.tags'

    " 配置 ctags 的参数
    let g:gutentags_ctags_extra_args = ['--fields=+niazS', '--extra=+q']
    let g:gutentags_ctags_extra_args += ['--c++-kinds=+px']
    let g:gutentags_ctags_extra_args += ['--c-kinds=+px']


" 异步运行
Plug 'skywind3000/asyncrun.vim'
" 异步项目构建
Plug 'skywind3000/asynctasks.vim'
    " 自动打开 quickfix window ，高度为 6
    let g:asyncrun_open = 6

    " 任务结束时候响铃提醒
    let g:asyncrun_bell = 1

    " 设置 F10 打开/关闭 Quickfix 窗口
    nnoremap <F10> :call asyncrun#quickfix_toggle(6)<cr>

" " 修改比较

" 文件切换

" 首页
Plug 'mhinz/vim-startify'
 
" 中文文档
Plug 'yianwillis/vimcdoc'

" 菜单
Plug 'skywind3000/vim-quickui'

" 快捷键提示
Plug 'liuchengxu/vim-which-key', { 'on': ['WhichKey', 'WhichKey!', 'WhichKeyVisual', 'WhichKeyVisual!'] }
    set timeoutlen=100
    let g:mapleader = "\<Space>"
    let g:maplocalleader = ','
    nnoremap <silent> <leader>      :<c-u>WhichKey '<Space>'<CR>
    nnoremap <silent> <localleader> :<c-u>WhichKey  ','<CR>
    vnoremap <silent> <leader> :<c-u>WhichKeyVisual '<Space>'<CR>
    vnoremap <localleader> :<c-u>WhichKeyVisual  ','<CR>

" 状态栏
Plug 'itchyny/lightline.vim'
" TODO config
    function! CocCurrentFunction() abort
        return get(b:, 'coc_current_function', '')
    endfunction
    function! NearestMethodOrFunction() abort
        return get(b:, 'vista_nearest_method_or_function', '')
    endfunction

    let g:lightline = {
        \     'colorscheme': 'wombat',
        \     'active': {
        \         'left': [ [ 'mode', 'paste' ],
        \                 ['readonly', 'filename', 'modified',  'cocstatus', 'currentfunction', 'method'] ]
        \     },
        \     'component_function': {
        \         'cocstatus': 'coc#status',
        \         'currentfunction': 'CocCurrentFunction',
        \         'method': 'NearestMethodOrFunction'
        \     },
        \ }

" 缩进线
Plug 'Yggdroot/indentLine'
    let g:indentLine_char_list = ['|', '¦', '┆', '┊']
    let g:indentLine_concealcursor = 'inc'
    let g:indentLine_conceallevel = 2

" 智能补全
Plug 'neoclide/coc.nvim', {'branch': 'release'}
   
" clangd.semanticHighlighting的依赖
Plug 'jackguo380/vim-lsp-cxx-highlight' 
    " vim 8.1的text prop
    let g:lsp_cxx_hl_use_text_props = 1

" 代码片段
Plug 'honza/vim-snippets'

" 更多文件类型支持
" Plug 'sheerun/vim-polyglot'

" 跳转
Plug 'easymotion/vim-easymotion'
    map ; <Plug>(easymotion-prefix)

" 多光标扩展
Plug 'mg979/vim-visual-multi', {'branch': 'master'}
    let g:VM_leader = ','

" 符号查看
Plug 'liuchengxu/vista.vim'

" 调试
if exists('win32')
    Plug 'puremourning/vimspector', {'do': 'py -3 install_gadget.py --all'}
elseif exists('unix')
    Plug 'puremourning/vimspector', {'do': 'python3 install_gadget.py --all'}
else
    Plug 'puremourning/vimspector'
endif


" " Powershell语法高亮
" Plug 'PProvost/vim-ps1'

Plug 'voldikss/vim-floaterm'
    let g:floaterm_keymap_toggle='<m-m>'

Plug 'voldikss/vim-browser-search'
    nmap <silent> <Leader>s <Plug>SearchNormal
    vmap <silent> <Leader>s <Plug>SearchVisual
    let g:browser_search_default_engine = 'baidu'

if has('win32')
    Plug 'voldikss/vim-find-everything'
endif

" Markdown预览
Plug 'iamcco/markdown-preview.nvim', { 'do': { -> mkdp#util#install() }, 'for': ['markdown', 'vim-plug']}

" Initialize plugin system
call plug#end()

" skywind3000/vim-quickui
    " enable to display tips in the cmdline
    let g:quickui_show_tip = 1
    let g:quickui_border_style = 2
    let g:quickui_color_scheme = 'system'
    " hit space twice to open menu
    nmap <leader>m :<c-u>call quickui#menu#open()<cr>
    " nnoremap <silent><space><space> :<c-u>call quickui#menu#open()<cr>

    call quickui#menu#reset()
    call quickui#menu#install('&debug', [
            \ [ "&debug", 'call vimspector#Launch()' ],
            \ [ "cl&ose debug", 'VimspectorReset' ],
            \ [ "--", '' ],
            \ [ "&continue", 'call vimspector#Continue()' ],
            \ [ "&stop", 'call vimspector#Stop()' ],
            \ [ "&restart", 'call vimspector#Restart()' ],
            \ [ "&pause", 'call vimspector#Pause()' ],
            \ [ "&toggle breakpoint", 'call vimspector#ToggleBreakpoint()' ],
            \ [ "add &function breakpoint", 'call vimspector#AddFunctionBreakpoint()' ],
            \ [ "step o&ver", 'call vimspector#StepOver()' ],
            \ [ "step i&nto", 'call vimspector#StepInto()' ],
            \ [ "step o&ut", 'call vimspector#StepOut()' ],
            \ ])

" 配色
silent! colorscheme molokai
" silent! colorscheme PaperColor

if !exists("my_auto_commands_loaded")
    let my_auto_commands_loaded=1
    let g:LargeFile=1024*1024*10
    augroup LargeFile
        autocmd BufReadPre * let f=expand("<afile>") |
                    \ if getfsize(f)>g:LargeFile |
                    \     set eventignore+=FileType |
                    \     let b:coc_enabled=0 |
                    \     setlocal noswapfile bufhidden=unload buftype=nowrite undolevels=-1 |
                    \ else |
                    \     set eventignore-=FileType |
                    \ endif
    augroup END
endif

" 通用快捷键
nnoremap <A-h> <C-w>h
nnoremap <A-j> <C-w>j
nnoremap <A-k> <C-w>k
nnoremap <A-l> <C-w>l
nnoremap <A-H> <C-w>H
nnoremap <A-J> <C-w>J
nnoremap <A-K> <C-w>K
nnoremap <A-L> <C-w>L

" terminal快捷键
" noremap <A-m> :vertical terminal<cr>
tnoremap <A-h> <C-w>h
tnoremap <A-j> <C-w>j
tnoremap <A-k> <C-w>k
tnoremap <A-l> <C-w>l
tnoremap <A-H> <C-w>H
tnoremap <A-J> <C-w>J
tnoremap <A-K> <C-w>K
tnoremap <A-L> <C-w>L
tnoremap <A-n> <C-w>N

" 剪切板快捷键
if has('clipboard')
    nnoremap <A-c> :let @*=expand('<cword>')<cr>
    nnoremap <A-f> :let @/=expand('<cword>')<cr>
    vnoremap <A-c> "*y
    vnoremap <A-x> "*d
    nnoremap <A-v> "*p
endif

if has('win32')
    source $VIM/vimfiles/win32.vim
elseif has('unix')
    source $VIM/vimfiles/linux.vim
endif
source $VIM/vimfiles/coc_config.vim

" 用于Windows Terminal光标设置为方块
let &t_SI.="\<Esc>[6 q" " 插入模式
let &t_SR.="\<Esc>[3 q" " 替换模式
let &t_EI.="\<Esc>[2 q" " 普通模式
